using System;
using System.Collections;
using System.Linq;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
namespace Microsoft.Phone.Controls
{
	public class SelectorSelectionAdapter : ISelectionAdapter
	{
		private Selector _selector;
		public event SelectionChangedEventHandler SelectionChanged;
		public event RoutedEventHandler Commit;
		public event RoutedEventHandler Cancel;
		private bool IgnoringSelectionChanged
		{
			get;
			set;
		}
		public Selector SelectorControl
		{
			get
			{
				return this._selector;
			}
			set
			{
				if (this._selector != null)
				{
					this._selector.remove_SelectionChanged(new SelectionChangedEventHandler(this.OnSelectionChanged));
				}
				this._selector = value;
				if (this._selector != null)
				{
					this._selector.add_SelectionChanged(new SelectionChangedEventHandler(this.OnSelectionChanged));
				}
			}
		}
		public object SelectedItem
		{
			get
			{
				if (this.SelectorControl != null)
				{
					return this.SelectorControl.get_SelectedItem();
				}
				return null;
			}
			set
			{
				this.IgnoringSelectionChanged = true;
				if (this.SelectorControl != null)
				{
					this.SelectorControl.set_SelectedItem(value);
				}
				if (value == null)
				{
					this.ResetScrollViewer();
				}
				this.IgnoringSelectionChanged = false;
			}
		}
		public IEnumerable ItemsSource
		{
			get
			{
				if (this.SelectorControl != null)
				{
					return this.SelectorControl.get_ItemsSource();
				}
				return null;
			}
			set
			{
				if (this.SelectorControl != null)
				{
					this.SelectorControl.set_ItemsSource(value);
				}
			}
		}
		public SelectorSelectionAdapter()
		{
		}
		public SelectorSelectionAdapter(Selector selector)
		{
			this.SelectorControl = selector;
		}
		private void ResetScrollViewer()
		{
			if (this.SelectorControl != null)
			{
				ScrollViewer scrollViewer = Enumerable.FirstOrDefault<ScrollViewer>(Enumerable.OfType<ScrollViewer>(this.SelectorControl.GetLogicalChildrenBreadthFirst()));
				if (scrollViewer != null)
				{
					scrollViewer.ScrollToVerticalOffset(0.0);
				}
			}
		}
		private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			if (this.IgnoringSelectionChanged)
			{
				return;
			}
			SelectionChangedEventHandler selectionChanged = this.SelectionChanged;
			if (selectionChanged != null)
			{
				selectionChanged.Invoke(sender, e);
			}
			this.OnCommit();
		}
		protected void SelectedIndexIncrement()
		{
			if (this.SelectorControl != null)
			{
				this.SelectorControl.set_SelectedIndex((this.SelectorControl.get_SelectedIndex() + 1 >= this.SelectorControl.get_Items().get_Count()) ? -1 : (this.SelectorControl.get_SelectedIndex() + 1));
			}
		}
		protected void SelectedIndexDecrement()
		{
			if (this.SelectorControl != null)
			{
				int selectedIndex = this.SelectorControl.get_SelectedIndex();
				if (selectedIndex >= 0)
				{
					Selector expr_1E = this.SelectorControl;
					expr_1E.set_SelectedIndex(expr_1E.get_SelectedIndex() - 1);
					return;
				}
				if (selectedIndex == -1)
				{
					this.SelectorControl.set_SelectedIndex(this.SelectorControl.get_Items().get_Count() - 1);
				}
			}
		}
		public void HandleKeyDown(KeyEventArgs e)
		{
			if (e == null)
			{
				throw new ArgumentNullException("e");
			}
			Key key = e.get_Key();
			if (key != 3)
			{
				if (key != 8)
				{
					switch (key)
					{
					case 15:
						this.SelectedIndexDecrement();
						e.set_Handled(true);
						return;
					case 16:
						break;
					case 17:
						if ((1 & Keyboard.get_Modifiers()) == null)
						{
							this.SelectedIndexIncrement();
							e.set_Handled(true);
							return;
						}
						break;
					default:
						return;
					}
				}
				else
				{
					this.OnCancel();
					e.set_Handled(true);
				}
				return;
			}
			this.OnCommit();
			e.set_Handled(true);
		}
		protected virtual void OnCommit()
		{
			this.OnCommit(this, new RoutedEventArgs());
		}
		private void OnCommit(object sender, RoutedEventArgs e)
		{
			RoutedEventHandler commit = this.Commit;
			if (commit != null)
			{
				commit.Invoke(sender, e);
			}
			this.AfterAdapterAction();
		}
		protected virtual void OnCancel()
		{
			this.OnCancel(this, new RoutedEventArgs());
		}
		private void OnCancel(object sender, RoutedEventArgs e)
		{
			RoutedEventHandler cancel = this.Cancel;
			if (cancel != null)
			{
				cancel.Invoke(sender, e);
			}
			this.AfterAdapterAction();
		}
		private void AfterAdapterAction()
		{
			this.IgnoringSelectionChanged = true;
			if (this.SelectorControl != null)
			{
				this.SelectorControl.set_SelectedItem(null);
				this.SelectorControl.set_SelectedIndex(-1);
			}
			this.IgnoringSelectionChanged = false;
		}
		public AutomationPeer CreateAutomationPeer()
		{
			if (this._selector == null)
			{
				return null;
			}
			return FrameworkElementAutomationPeer.CreatePeerForElement(this._selector);
		}
	}
}
